package android.medicalcloud.com.yiapp.yilian.utils.android;


import android.content.Context;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.configer.contance.Constance;
import android.os.Environment;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FileUtils {
    public static String SDPATH = Environment.getExternalStorageDirectory() + "/";

    private static int FILESIZE = 4 * 1024;

    public FileUtils() {
        //得到当前外部存储设备的目录( /SDCARD )
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }

    /**
     * 在SD卡上创建文件
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static File createSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
        file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     *
     * @param dirName
     * @return
     */
    public static File createSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        if (!dir.exists())
            dir.mkdir();
        return dir;
    }


    public static synchronized String getCacheDirectory(Context context) {
        String sRootDirectory;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            sRootDirectory = Environment.getExternalStorageDirectory().toString() + "/Android/data/" + context.getPackageName();
        } else {
            sRootDirectory = context.getFilesDir().toString();

        }
        return sRootDirectory;
    }
    public static String getDataBasePath(Context context){
        String sRootDirectory;
        sRootDirectory = context.getDatabasePath("..").getParentFile().getAbsolutePath();
        Logger.e("---getDataBasePath---"+sRootDirectory);
        return sRootDirectory;
    }
    public static File createTmpFile(Context context) {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载

            File pic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            if (!pic.exists()) {
                pic.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(pic, fileName + ".jpg");
            return tmpFile;
        } else {
            File cacheDir = context.getCacheDir();
            if (!cacheDir.exists()) {
                cacheDir.mkdirs();
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
            String fileName = "multi_image_" + timeStamp + "";
            File tmpFile = new File(cacheDir, fileName + ".jpg");
            return tmpFile;
        }

    }

    public static void mergeFileWithPage(Context c, String dst)
            throws IOException {
        OutputStream out = new FileOutputStream(new File(Constance.CACHE_DATABASE+"city.db"));
        byte[] buffer = new byte[1024];
        InputStream in;
        int readLen = 0;
        in = c.getAssets().open("city.db");
        while ((readLen = in.read(buffer)) != -1) {
            out.write(buffer, 0, readLen);
        }
        Logger.e("---mergeFileWithPage---");
        out.flush();
        in.close();
        out.close();
    }
}
